﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Data;

namespace UniMana.Win.Systems
{
    public partial class FrmUserList_SubInfo : DevExpress.XtraEditors.XtraForm
    {
        Personnel_PersonnelInfo m_PersionelInfo;
        //System_UserInfo m_SystemUserInfo;
        public FrmUserList_SubInfo()
        {
            
            InitializeComponent();

            //m_SystemUserInfo = new System_UserInfo(_guiSystemUserKey);

            if (SysGlobalVar.g_sta_sysUserLogin.Properties.Personnel_PersonnelKey != null)
            {
                m_PersionelInfo = new Personnel_PersonnelInfo(SysGlobalVar.g_sta_sysUserLogin.Properties.Personnel_PersonnelKey.Value);
            }
            else
            {
                MessageBox.Show("Tài khoản không tải được thông tin. Liên hệ Quản trị hệ thống để kiểm tra lại");
                this.Visible = false;
            }
            LoadData();
        }

        void LoadData()
        {
            txtUserNameNB.Text = SysGlobalVar.g_sta_sysUserLogin.Properties.UserName;
            if (m_PersionelInfo != null)
            {
                lcgPersonalInfo.LoadOneRecord(m_PersionelInfo.m_bds);

                ////txtPassword.Text = SysGlobalVar.g_sta_sysUserLogin.Properties.Password;
                //txtLastName.Text = m_PersionelInfo.Properties.LastName;
                //txtName.Text = m_PersionelInfo.Properties.Name;
                //txtIdentityNo.Text = m_PersionelInfo.Properties.IdentityNo;
                //txtPlaceOfBirth.Text = m_PersionelInfo.Properties.PlaceOfBirth;
                //txtEmail.Text = m_PersionelInfo.Properties.Email;
                //txtMobilePhone.Text = m_PersionelInfo.Properties.CellPhone;
                //txtTermAddress.Text = m_PersionelInfo.Properties.TempAddr;
                if (m_PersionelInfo.Properties.Gender != null)
                {
                    if (m_PersionelInfo.Properties.Gender.Value == UniMana.Utils.Configs.DefaultConfig.MALE)
                    {
                        ckeMale.Checked = true;//UniMana.Utils.Config.DefaultConfig.MALE;
                        this.ckeFemale.Checked = false;
                    }

                    if (m_PersionelInfo.Properties.Gender.Value == UniMana.Utils.Configs.DefaultConfig.FEMALE)
                    {
                        ckeMale.Checked = false;
                        this.ckeFemale.Checked = true;//UniMana.Utils.Config.DefaultConfig.FEMALE;
                    }

                }

                this.dteBirthday.EditValue = m_PersionelInfo.Properties.Birthday;

                lueCatalogue_Department.GetData(Catalogue_DepartmentData.GetList());

                lueCatalogue_Department.EditValue = m_PersionelInfo.Properties.Catalogue_DepartmentKey;
            }
        }

        //private bool DataValidate()
        //{
        //    if (this.dteBirthday.EditValue != null)
        //    {

        //    }
        //}

        private void btnSave_Click(object sender, EventArgs e)
        {

            m_PersionelInfo.Properties.TempAddr = mneTempAddr.Text;
            m_PersionelInfo.Properties.CellPhone = txtCellPhone.Text;
            if (txtEmail.Text.IndexOf('@') != -1)
            {
                m_PersionelInfo.Properties.Email = txtEmail.Text;
            }
            else
            {
                MessageBox.Show("Email không hợp lệ vui lòng kiểm tra lại");
            }

            m_PersionelInfo.Save();

            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
           
        }

        private void ptePicture_EditValueChanged(object sender, EventArgs e)
        {
            
        }

        private void ptePicture_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog odg = new OpenFileDialog();
                odg.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
                odg.ShowDialog(this);
                string imageFile = odg.FileName;

                if (imageFile != string.Empty)
                {
                    Image img = Image.FromFile(imageFile);

                    ptePicture.Image = Image.FromFile(imageFile);
                    img = img.GetThumbnailImage(240, 480, null, new System.IntPtr());
                    ptePicture.Image = img;
                    m_PersionelInfo.m_bds.EndEdit();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void pteSignature_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog odg = new OpenFileDialog();
                odg.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
                odg.ShowDialog(this);
                string imageFile = odg.FileName;

                if (imageFile != string.Empty)
                {
                    Image img = Image.FromFile(imageFile);

                    pteSignature.Image = Image.FromFile(imageFile);
                    img = img.GetThumbnailImage(240, 480, null, new System.IntPtr());
                    pteSignature.Image = img;
                    m_PersionelInfo.m_bds.EndEdit();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void llbChangePassword_Click(object sender, EventArgs e)
        {
            FrmChangePassword f = new FrmChangePassword();
            f.ShowDialog();
        }

       

        
    }
}